package test27.homework;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Homework03 {
    public static void main(String[] args) {
        String content = "http://www.sohu.com:8080/abc/xxx/yyy/////index.htm";
        String regStr = "^(http)://([a-zA-Z]+\\.)+[a-zA-Z]+:(\\d)+(/[a-zA-Z]*)+\\.[a-zA-Z]+$";
        regStr = "^([a-zA-Z]+)://[a-zA-Z.]+:(\\d+)[/A-Za-z]+\\.[\\w]+$";   //不能拿到文件
        regStr = "^([a-zA-Z]+)://([a-zA-Z.]+):(\\d+)[/\\w]+/([\\w.@#$%]+)$"; //简化版

        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        //整体匹配, 如果匹配成功，可以通过group(x), 获取对应分组的内容
        if (matcher.matches()){
            System.out.println("整体匹配 "+matcher.group(0));
            System.out.println("协议 "+matcher.group(1));
            System.out.println("域名 "+matcher.group(2));
            System.out.println("端口 "+matcher.group(3));
            System.out.println("文件 "+matcher.group(4));
        }else {
           System.out.println("没有匹配成功");
        }
    }
}
